<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请选择" clearable size="small">
          <el-option
            v-for="item in classList"
            :key="item.id"
            :label="item.label"
            :value="item.id"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input type="text" v-model="queryParams.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >分配照片</el-button>
      </el-col>
<!--      &lt;!&ndash;      <el-col :span="1.5">&ndash;&gt;-->
<!--      &lt;!&ndash;        <el-button&ndash;&gt;-->
<!--      &lt;!&ndash;          type="warning"&ndash;&gt;-->
<!--      &lt;!&ndash;          icon="el-icon-download"&ndash;&gt;-->
<!--      &lt;!&ndash;          size="mini"&ndash;&gt;-->
<!--      &lt;!&ndash;          @click="handleExport"&ndash;&gt;-->
<!--      &lt;!&ndash;        >导出</el-button>&ndash;&gt;-->
<!--      &lt;!&ndash;      </el-col>&ndash;&gt;-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>
    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="学号" align="center" prop="code" />
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="照片数量" align="center" prop="imgCount" />
      <el-table-column label="发布时间" align="center" prop="lastDate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.readTime == null"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
          >管理</el-button>
<!--          <el-button v-if="scope.row.readTime == null"-->
<!--                     size="mini"-->
<!--                     type="text"-->
<!--                     icon="el-icon-delete"-->
<!--                     @click="handleDelete(scope.row)"-->
<!--                     v-hasPermi="['oa:report:remove']"-->
<!--          >删除</el-button>-->

        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 管理对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-row :gutter="10" class="mb8">
<!--        <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="primary"-->
<!--            icon="el-icon-plus"-->
<!--            size="mini"-->
<!--            @click="handleAddphoto"-->
<!--          >新增</el-button>-->
<!--        </el-col>-->
        <el-col :span="1.5">
          <el-button
            type="warning"
            icon="el-icon-circle-close"
            size="mini"
            :disabled="multiple"
            @click="delphoto"
          >删除</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="photoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" width="50">
          <template slot-scope="scope">
            {{scope.$index + (queryParamsphoto.pageNum-1)*queryParamsphoto.pageSize +1}}
          </template>
        </el-table-column>
        <el-table-column label="图片" align="center" prop="imgUrl">
          <template slot-scope="scope">
            <el-image
              style="width: 120px;height: 60px;"
              :src="scope.row.imgUrl"
              lazy
              :preview-src-list="[scope.row.imgUrl]">
            </el-image>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="delphoto(scope.row)"
               style="margin-right: 5px"
            >删除</el-button>
<!--            <el-button-->
<!--              size="mini"-->
<!--              type="text"-->
<!--              icon="el-icon-edit"-->
<!--              @click="handleUpdatePhoto(scope.row)"-->
<!--              v-hasPermi="['base:teacher:remove']"-->
<!--            >修改</el-button>-->

            <el-popover
              placement="right"
              width="400"
              trigger="click" @show="init(scope.row)" ref="popovertable">
              <el-table :data="currentStudentList" @row-click="clicktabel" height="280">
                <el-table-column property="name" label="姓名"></el-table-column>
                <el-table-column property="code" label="学号"></el-table-column>
              </el-table>
              <pagination
                v-show="edittotal>0"
                :total="edittotal"
                :page.sync="editqueryParams.pageNum"
                :limit.sync="editqueryParams.pageSize"
                @pagination="init"
              />
              <el-button slot="reference" size="mini" type="text" icon="el-icon-edit">修改</el-button>
            </el-popover>


          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="totalPhoto>0"
        :total="totalPhoto"
        :page.sync="queryParamsphoto.pageNum"
        :limit.sync="queryParamsphoto.pageSize"
        @pagination="getphotolist"
      />

      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="cancel">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加照片对话框 -->
    <AddgrowPhoto ref="addgrowphoto" @getList="getList"></AddgrowPhoto>

  </div>
</template>

<script>

import { addDevice, delDevice, listdevice, updateDevice } from '@/api/base/device'
import { addStudenPhoto, delstudenPhoto, editStudenPhoto, listgrowphoto, updatesphotoList,getAlbumByClassId } from '@/api/base/grow'
import { selectClazz } from '@/api/base/clazz'
import { listPhotogl } from '@/api/base/gardensystem'
import AddgrowPhoto from '@/components/AddgrowPhoto/index'
export default {
  name: "phonegl",
  dicts: ['year_type','classname_type'],
  components:{ AddgrowPhoto },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        deviceName: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        deviceType: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        vendorId:[
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        deviceCode:[
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        schoolCode:[
          { required: true, message: "不能为空", trigger: "blur" }
        ],
      },
      photoList:[],//弹窗照片数据
      // pageSizePhoto:10,
      // currentPagePhoto:1,
      totalPhoto:0,
      titlephoto:'',
      openphoto:false,
      // formphoto:{},
      // movie:null,//视频
      // urlistmovie:[],
      // dialogVisiblevideo:false,
      // uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      classList:[],//班级
      queryParamsphoto: {
        pageNum: 1,
        pageSize: 10,
        // stuCode:null,
        stuId:null
      },
      // classPhotolist:[],//班级圈照片列表
      // clastotal:0,
      // queryParamsclass:{
      //   pageNum: 1,
      //   pageSize: 10,
      //   classId:null,
      // },
      showaddgrow:false,
      editqueryParams:{
        pageNum: 1,
        pageSize: 10,
        classId:null
      },
      edittotal:0,
      currentStudentList:[],
      url:null,
      id:null,//原来的学生号
      flagType:null
    };
  },
  created() {
    this.getList();
    //班级
    this.getQueryClazzList();
  },
  methods: {
    /* 修改 调用同班学生列表 */
    async init(row){
      this.flagType = null
      if(row.stuId){
        this.flagType = true
      }else{
        this.flagType = false
      }
      console.log(row,'----------------------<row');
      try {
        //保存图片路径
        this.id=row.id
        if(row){
          this.url=row.imgUrl
        }
        this.loading = true;
        const data=await listgrowphoto(this.editqueryParams)
        // data.rows.forEach((item,index)=>{
        //   if(item.code==row.stuCode){
        //     console.log(row.stuCode,'-------row.stuCode');
        //     data.rows.splice(index,1)
        //   }
        // });
        this.currentStudentList=data.rows
        this.edittotal = data.total;
        this.loading = false;
      }catch (e) {
        console.log(e)
      }
    },
    /**修改 选择学生*/
    async clicktabel(e){

      // debugger;
      let params={
        id:this.id,
        // imgUrl:this.url,
        toStuId:e.stuId
      }
      const data = await editStudenPhoto(params).then(res=>{
        this.msgSuccess("修改成功");
        if(this.flagType){
          this.getphotolist(1)
        }else{
          this.getphotolist(2)
        }
        this.getList();
        console.log(this.$refs.popovertable)
        this.$refs.popovertable.doClose();
        document.body.click()

      })
      // if(data.code==200){
      //
      // }
    },
    /** 查询班级列表 */
    async getQueryClazzList() {
      try{
        this.loading = true;
        const {rows}=await selectClazz();
        this.classList=rows.map(item=>{
          return {
            id:item.id,
            label:item.title,
          }
        })
        this.loading = false;
      }catch (e) {
        console.log(e)
      }
    },
    /** 查询学生列表 */
    async getList() {
      try {
        this.loading = true;
        const data=await listgrowphoto(this.queryParams)
        this.studentList = data.rows;
        this.total = data.total;
        this.loading = false;
      }catch (e) {
        console.log(e)
      }
    },
    /**获取学生相册**/
    async getphotolist(val){
      try {
        const data= val === 1  ? await updatesphotoList(this.queryParamsphoto) : await getAlbumByClassId(this.queryParamsphoto)
        this.photoList=data.rows
        this.totalPhoto=data.total
      }catch (e) {
        console.log(e)
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.editqueryParams.classId=row.classId
      // this.queryParamsphoto.stuCode=row.code
      this.queryParamsphoto.stuId = row.stuId
      this.queryParamsphoto.classId = row.classId
      if(row.stuId){
        // 学生
        this.getphotolist(1).then(res=>{
          this.open = true;
          this.title = "修改照片";
        })
      }else{
        // 班级
        this.getphotolist(2).then(res=>{
          this.open = true;
          this.title = "修改照片";
        })
      }

    },
    //删除学生照片
    delphoto(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delstudenPhoto(ids);
      }).then(() => {
        if(row.stuId){
          this.getphotolist(1);
        }else{
        this.getphotolist(2);
        }
        this.msgSuccess("删除成功");
      })
    },
    /**调用朋友圈列表照片**/
    // async getClassList(){
    //   try {
    //     this.classPhotolist=[]
    //     const data=await listPhotogl(this.queryParams);
    //     data.rows.map(item=>{
    //       console.log(item.imgList!='')
    //       if(item.imgList!=''){
    //         this.classPhotolist=this.classPhotolist.concat(item.imgList)
    //       }
    //     })
    //     this.clastotal=data.total
    //   }catch (e) {
    //     console.log(e)
    //   }
    // },
    /** 新增照片按钮操作 */
    // handleAddphoto() {
    //   this.reset();
    //   //调用朋友圈照片列表
    //   this.getClassList()
    //   this.openphoto = true;
    //   this.titlephoto = "添加照片";
    // },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceName: null,
        schoolCode: null,
        vendorId: null,
        deviceType: null,
        deviceCode: null,
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.addgrowphoto.openPhoto();
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDevice(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delDevice(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },

    // 表单重置
    // resetphoto() {
    //   this.formphoto = {
    //
    //   };
    //   this.resetForm("formphoto");
    // },
    // 取消照片按钮
    cancelPhoto() {
      this.openphoto = false;
      this.resetphoto();
    },
  }
};
</script>
<style lang="scss" scoped>
.el-dialog .el-image{
  left: 50%;
  transform: translate(-50%,0);
}
.radiostyle{
  position: absolute;
  z-index: 10;
  right: 10px;
  top: 10px;
}
</style>
